package com.rk.jarjuggler.model;

import javax.swing.tree.TreeNode;
import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import com.intellij.util.enumeration.ArrayListEnumeration;

public class DirNode implements TreeNode, Comparable<DirNode>, Serializable {
    private static final long serialVersionUID = 1L;

    private DirNode parent;

    private String url;
    private String name = "";
    private int level = 0;

    private ArrayList<DirNode> dirNodeChildren = new ArrayList<DirNode>();

    public DirNode() {
    }

    protected DirNode(DirNode theParent, String name) {
        this.parent = theParent;
        this.name = name;

        if (theParent != null){
            theParent.addChildDir(this);
            level = theParent.level + 1;
        }
    }

    public DirNode(DirNode parent, String name, String url) {
        this(parent, name);
        this.url = url;
    }

    public void addChildDir(DirNode node) {
        dirNodeChildren.add(node);
        Collections.sort(dirNodeChildren);
    }

    // implement TreeNode

    public TreeNode getChildAt(int i) {
        return dirNodeChildren.get(i);
    }

    public int getChildCount() {
        return dirNodeChildren.size();
    }

    public int getLevel() {
        return level;
    }

    protected DirNode getDirParent() {
        return parent;
    }

    public TreeNode getParent() {
        return parent;
    }

    public int getIndex(TreeNode treeNode) {
        return dirNodeChildren.indexOf(treeNode);
    }

    public boolean getAllowsChildren() {
        return true;
    }

    public boolean isLeaf() {
        return false;
    }

    public ArrayList<DirNode> getDirNodeChildren() {
        return dirNodeChildren;
    }

    public Enumeration children() {
        return new ArrayListEnumeration(dirNodeChildren);
    }

    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public int compareTo(DirNode dirNode) {
        return Collator.getInstance().compare(getName(), dirNode.getName());
    }

    public boolean passesFilter(String searchText, boolean hasJar, boolean hasSrc, boolean hasJavadoc) {
        for (DirNode dirNode : dirNodeChildren) {
            if (dirNode.passesFilter(searchText, hasJar, hasSrc, hasJavadoc)) {
                return true;
            }
        }
        return false;
    }

    public String getFullName() {
        if (getDirParent() == null) {
            return null;
        } else {
            String parentFullName = getDirParent().getFullName();
            if (parentFullName != null){
                return parentFullName + " : " + getName();
            } else {
                return getName();
            }
        }
    }
}
